package ph.com.gs3.formalistics.presenter.fragments.views;

import ph.com.gs3.formalistics.R;
import ph.com.gs3.formalistics.presenter.adapters.DocumentListItemViewAdapter;
import ph.com.gs3.formalistics.view.valueobjects.UserActionListViewItem;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

/**
 * 
 * @author Ervinne Sodusta
 */
public class DocumentListViewFragment extends Fragment {

	public static final String TAG = DocumentListViewFragment.class.getSimpleName();

	private ListView lvDocumentList;

	private SwipeRefreshLayout swipeRefreshLayout;
	private int colorRes1 = android.R.color.holo_blue_bright;
	private int colorRes2 = android.R.color.holo_green_dark;
	private int colorRes3 = android.R.color.holo_orange_dark;
	private int colorRes4 = android.R.color.holo_purple;

	private DocumentListViewFragmentActionListener actionListener;
	private DocumentListItemViewAdapter adapter;

	public static DocumentListViewFragment createInstance(String header,
	        DocumentListItemViewAdapter adapter) {

		DocumentListViewFragment instance = new DocumentListViewFragment();
		instance.adapter = adapter;

		// TODO: if displaying of header is necessary, use the header variable

		return instance;

	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View rootView = inflater.inflate(R.layout.fragment_document_list, container, false);
		initializeViews(rootView);

		return rootView;
	}

	private void initializeViews(View rootView) {

		/*
		 * tvHeader = (TextView) rootView.findViewById(R.id.DL_tvHeader);
		 * tvHeader.setText(header);
		 */

		swipeRefreshLayout = (SwipeRefreshLayout) rootView.findViewById(R.id.swipe_container);
		swipeRefreshLayout.setColorScheme(colorRes1, colorRes2, colorRes3, colorRes4);
		swipeRefreshLayout.setSoundEffectsEnabled(true);
		swipeRefreshLayout.setOnRefreshListener(new OnRefreshListener() {

			@Override
			public void onRefresh() {
				actionListener.onRefreshCommand();
			}
		});
		lvDocumentList = (ListView) rootView.findViewById(R.id.DL_lvDocumentList);
		lvDocumentList.setAdapter(adapter);

		lvDocumentList.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				// TODO: add necessary ui updates here

				if (actionListener != null) {

					UserActionListViewItem userAction = (UserActionListViewItem) adapter
					        .getItem(position);
					actionListener.onUserActionListItemSelected(userAction);
				}
			}

		});

	}

	public void setActionListener(DocumentListViewFragmentActionListener actionListener) {
		this.actionListener = actionListener;
	}

	public void startSwipeRefresh() {
		if (swipeRefreshLayout != null) {
			swipeRefreshLayout.setRefreshing(true);
		}
	}

	public void stopSwipeRefresh() {
		if (swipeRefreshLayout != null) {
			swipeRefreshLayout.setRefreshing(false);
		}
	}

	public static interface DocumentListViewFragmentActionListener {
		public void onRefreshCommand();

		public void onUserActionListItemSelected(UserActionListViewItem userActionListViewItem);

	}

}
